package model.core;

import java.util.*;

import model.map.*;

public class PlacementInstantiation {
	private int xDim;
	private int yDim;
	private Map<MapPoint, Terrain> terrMap;
	private Map<MapPoint, String> decMap;
	private Map<MapPoint, List<Obstacle>> obstMap;
	private String shroud;
	private String view;
	
	private LineParser lineParser = new LineParser();
	
	public PlacementInstantiation(){
		xDim = 0;
		yDim = 0;
		terrMap = new HashMap<MapPoint, Terrain>();
		decMap = new HashMap<MapPoint, String>();
		obstMap = new HashMap<MapPoint, List<Obstacle>>();
		shroud = new String();
		view = new String();
	}

	//"(x,y);terrainName;imageID"
	public void addToTerrain(String id){
		ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
		ArrayList<String> t2 = lineParser.parseStringOnCommaAndRemoveParen(tokens.get(0));
		int xPos = Integer.parseInt(t2.get(0));
		int yPos = Integer.parseInt(t2.get(1));
		String terrainName = tokens.get(1);
		String graphic = tokens.get(2);
		Terrain t = null;
		if (terrainName.equals("Grass")){
			t = new Grass(graphic);
		}
		else if (terrainName.equals("Mountain")){
			t = new Moutain(graphic);
		}
		else if (terrainName.equals("Mud")){
			t = new Mud(graphic);
		}
		else if (terrainName.equals("Water")){
			t = new Water(graphic);
		}
		terrMap.put(new MapPoint(xPos, yPos), t);
		//System.out.println(t.getGraphicID());
	}
	
	//"(x,y);decalName"
	public void addToDecals(String id){
		ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
		ArrayList<String> t2 = lineParser.parseStringOnCommaAndRemoveParen(tokens.get(0));
		int xPos = Integer.parseInt(t2.remove(0));
		int yPos = Integer.parseInt(t2.remove(0));
		String decalName = tokens.get(1);
		decMap.put(new MapPoint(xPos, yPos), decalName);
	}
	
	//"(x,y);(obName,imageID);(obName,imageID);(obName,imageID);..."
	public void addToObstacles(String id){
		//System.out.println("**" + id);
		ArrayList<String> tokens = lineParser.parseStringOnSemicolon(id);
		ArrayList<String> t2 = lineParser.parseStringOnCommaAndRemoveParen(tokens.remove(0));
		int xPos = Integer.parseInt(t2.remove(0));
		int yPos = Integer.parseInt(t2.remove(0));
		ArrayList<Obstacle> obs = new ArrayList<Obstacle>();
		for (String s : tokens){
			//System.out.println("!" + s);
			ArrayList<String> obPair = lineParser.parseStringOnCommaAndRemoveParen(s);
			obs.add(new Obstacle(obPair.get(0), obPair.get(1)));
		}
		obstMap.put(new MapPoint(xPos, yPos), obs);
	}
	
	public void setShroudedGraphic(String id){
		this.shroud = id;
	}
	
	public void setViewableGraphic(String id){
		this.view = id;
	}
	
	public void setXDim(int x){
		this.xDim = x;
	}
	
	public void setYDim(int y){
		this.yDim = y;
	}
	
	public GameMap createMap(){
		System.out.println(xDim);
		GameMap g = new GameMap(xDim, yDim);
		g.buildBaseMap(terrMap, decMap, obstMap);		
		return g;
	}
	
}
